//
//  MXHomeEquimentListController.m
//  HNBaseProject
//
//  Created by xujie on 2024/6/5.
//

#import "MXHomeEquimentListController.h"
#import "MxPublicNav.h"
#import "MXHomeEquimentListCell.h"
#import "MXHomeListModel.h"
#import "MXShiftInController.h"
@interface MXHomeEquimentListController () <MxNavigationViewDelegate,UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView         *listTab;
@property (nonatomic, strong) MxPublicNav *nav;
@property (nonatomic, strong) NSMutableArray *listArray;
@end

@implementation MXHomeEquimentListController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadList) name:@"transferdevice" object:nil];
    self.listArray = [[NSMutableArray alloc] init];
    [self.listArray addObjectsFromArray:self.hmodel.deviceList];
    [self createUI];

}

- (void)reloadList {
    
}

- (void)createUI {
    
    self.nav = [[MxPublicNav alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kNavBarHeight)];
    self.nav.titleLab.text = @"家庭设备";
    self.nav.delegate = self;
    [self.view addSubview:self.nav];
    
    MJWeakSelf
    UILabel *noticleLab = [[UILabel alloc] init];
    noticleLab.text = [NSString stringWithFormat:@"以下是“%@”的设备",self.hmodel.familyName];//@"";
    [self.view addSubview:noticleLab];
    noticleLab.font = PFRFontMedium(14);
    noticleLab.textColor = [UIColor dc_colorWithHexString:@"#000000" alpha:0.9];
    [noticleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(30);
        make.top.offset(kNavBarHeight + 23);
    }];
    
    UILabel *refuseLab = [[UILabel alloc] init];
    refuseLab.userInteractionEnabled = YES;
    refuseLab.textAlignment = NSTextAlignmentCenter;
    refuseLab.text = @"移出设备";
    refuseLab.textColor = [UIColor colorWithHexString:@"#DF0505"];
    refuseLab.font = PFRFontMedium(16);
    refuseLab.backgroundColor = [UIColor colorWithHexString:@"#EEEEEE"];
    refuseLab.layer.cornerRadius = 24;
    refuseLab.layer.masksToBounds = YES;
    [refuseLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf removeEquipment];
    }]];
    [self.view addSubview:refuseLab];
    [refuseLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(48);
        make.bottom.offset(-48);
        make.left.offset(15);
        make.right.offset(-15);
    }];
    
    UILabel *acceptLab = [[UILabel alloc] init];
    acceptLab.userInteractionEnabled = YES;
    acceptLab.textAlignment = NSTextAlignmentCenter;
    acceptLab.text = @"从其他家庭移入设备";
    acceptLab.textColor = [UIColor colorWithHexString:@"#FEFEFE"];
    acceptLab.font = PFRFontMedium(16);
    acceptLab.backgroundColor = [UIColor colorWithHexString:@"#3278F6"];
    acceptLab.layer.cornerRadius = 24;
    acceptLab.layer.masksToBounds = YES;
    [acceptLab addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithActionBlock:^(id  _Nonnull sender) {
        [weakSelf addEquipment];
    }]];
    [self.view addSubview:acceptLab];
    [acceptLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(48);
        make.bottom.mas_equalTo(refuseLab.mas_top).offset(-17);
        make.left.offset(15);
        make.right.offset(-15);
    }];
    
    [self.view addSubview:self.listTab];
    [self.listTab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.mas_equalTo(noticleLab.mas_bottom).offset(10);
        make.bottom.mas_equalTo(acceptLab.mas_top).offset(-5);
    }];
    
}

#pragma mark - ________ UITableView ________

- (UITableView *)listTab {
    
    if (nil == _listTab) {
        _listTab = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listTab.backgroundColor = [UIColor colorWithHexString:@"#f5f5f5"];
        _listTab.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTab.delegate = self;
        _listTab.dataSource = self;
        _listTab.rowHeight = 80.0;
        _listTab.showsVerticalScrollIndicator = NO;
        _listTab.showsHorizontalScrollIndicator= NO;
        [_listTab registerClass:[MXHomeEquimentListCell class] forCellReuseIdentifier:NSStringFromClass([MXHomeEquimentListCell class])];
        if (@available(iOS 11.0, *)) {
            _listTab.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        if (@available(iOS 15.0, *)) {
            _listTab.sectionHeaderTopPadding = 0;
        }
    }
    return _listTab;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MXHomeListModel *model = self.listArray[indexPath.row];
    MXHomeEquimentListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MXHomeEquimentListCell class])];
    cell.titleLab.text = model.deviceLocalName;
    return cell;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.listArray.count;
}

- (void)removeEquipment {
    
}

- (void)addEquipment {
    MXShiftInController *shiftVc = [[MXShiftInController alloc] init];
    shiftVc.hModel = self.hmodel;
    [self.navigationController pushViewController:shiftVc animated:YES];
}

-(void)leftTap {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self dc_navBarHidden:YES];
}

@end
